﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyMainSite.Models;
using System.Xml;
using System.IO;
using System.Globalization;
using DotNetX;

namespace MyMainSite.Controllers
{
    public class RSSController : Controller
    {
        //
        // GET: /RSS/

        public ContentResult Index()
        {
            RichardsSiteEntities dc = new RichardsSiteEntities();

            var entries = from e in dc.BlogEntries
                          orderby e.DateCreated descending
                          select e;

            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", String.Empty, String.Empty));

            XmlElement elementRSS = doc.CreateElement("rss");
            doc.AppendChild(elementRSS);

            XmlAttribute attributeRSSVersion = doc.CreateAttribute("version");
            attributeRSSVersion.Value = "2.0";
            elementRSS.Attributes.Append(attributeRSSVersion);

                XmlElement elementChannel = doc.CreateElement("channel");
                elementRSS.AppendChild(elementChannel);

                    XmlElement elementChannelTitle = doc.CreateElement("title");
                    elementChannelTitle.InnerText = "RichardsComputer.co.uk";
                    elementChannel.AppendChild(elementChannelTitle);

                    XmlElement elementChannelLink = doc.CreateElement("link");
                    elementChannelLink.InnerText = this.Request.Url.QualifiedHost();
                    elementChannel.AppendChild(elementChannelLink);

                    XmlElement elementChannelDescription = doc.CreateElement("description");
                    elementChannelDescription.InnerText = "The blog, projects and CV of Richard Hughes";
                    elementChannel.AppendChild(elementChannelDescription);

                    XmlElement elementChannelTTL = doc.CreateElement("ttl");
                    elementChannelTTL.InnerText = "1440";
                    elementChannel.AppendChild(elementChannelTTL);

                    foreach (var e in entries)
                    {
                        XmlElement elementChannelItem = doc.CreateElement("item");
                        elementChannel.AppendChild(elementChannelItem);

                            XmlElement elementChannelItemTitle = doc.CreateElement("title");
                            elementChannelItem.AppendChild(elementChannelItemTitle);

                            if (e.Blog.Project == null)
                            {
                                elementChannelItemTitle.InnerText = e.Title;
                            }
                            else
                            {
                                elementChannelItemTitle.InnerText = String.Format("{0} - {1}", e.Blog.Project.Name, e.Title);
                            }

                            string url = String.Format("{0}blog/index/{1}/{2}/{3}",
                                this.Request.Url.QualifiedHost(),
                                e.DateCreated.Year,
                                CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(e.DateCreated.Month),
                                e.FriendlyUrl);

                            XmlElement elementChannelItemLink = doc.CreateElement("link");
                            elementChannelItem.AppendChild(elementChannelItemLink);
                            elementChannelItemLink.InnerText = url;

                            XmlElement elementChannelItemGUID = doc.CreateElement("guid");
                            elementChannelItem.AppendChild(elementChannelItemGUID);
                            elementChannelItemGUID.InnerText = url;

                            XmlElement elementChannelItemDescription = doc.CreateElement("description");
                            elementChannelItem.AppendChild(elementChannelItemDescription);

                                XmlCDataSection elementChannelItemDescriptionCDATA = doc.CreateCDataSection(e.BodyHtml);
                                elementChannelItemDescription.AppendChild(elementChannelItemDescriptionCDATA);
                    }

            StringWriter sw = new StringWriter();
            XmlTextWriter writer = new XmlTextWriter(sw);
            doc.WriteTo(writer);

            return this.Content(sw.ToString(), "application/rss+xml");
        }
    }
}
